package com.yaj.mesif.controller;

import com.yaj.mesif.dto.ErrIfi1006Dto;
import com.yaj.mesif.dto.Ifi1006Dto;
import com.yaj.mesif.dto.TiItemDto;
import com.yaj.mesif.service.IFI1001Service;
import com.yaj.mesif.service.IFI1006Service;
import com.yaj.mesif.unil.ErrorCodeEnum;
import com.yaj.mesif.unil.RespBean;
import com.yaj.mesif.unil.SucessCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


@Api(tags = "IfiController")
@Slf4j
@RestController
@RequestMapping("/mesif")
public class IfiController {
    @Autowired
    private IFI1001Service ifi1001Service;

    @Autowired
    private IFI1006Service ifi1006Service;

    @ApiOperation(value = "登录品目情报")
    @PostMapping(value = "/ifi1001")
    public RespBean ifi1001(@RequestBody List<TiItemDto> tiItemDtos){
        return ifi1001Service.ifi1001(tiItemDtos);
    }

    @ApiOperation(value = "登陆指图书情报")
    @PostMapping(value = "/ifi1006")
    public RespBean ifi1006(@RequestBody List<Ifi1006Dto> ifi1006Dtos){
        RespBean respBean = null;
        List<ErrIfi1006Dto> errIfi1006Dtos = new ArrayList<ErrIfi1006Dto>();
        boolean resultFlg = true;
        try{
            for (Ifi1006Dto ifi1006Dto:ifi1006Dtos){
                try{
                    ifi1006Service.ifi1006(ifi1006Dto);
                    log.info(ifi1006Dto.getTiOrderDto().getOrderid());
                }catch (Exception e){
                    resultFlg = false;
                    ErrIfi1006Dto errIfi1006Dto = new ErrIfi1006Dto();
                    errIfi1006Dto.setIfi1006Dto(ifi1006Dto);
                    errIfi1006Dto.setErrorMessage(e.getMessage());
                    errIfi1006Dtos.add(errIfi1006Dto);
                }
            }
        }catch (Exception e){
            log.error(e.getMessage());
        }

        if(!resultFlg){
            log.error(errIfi1006Dtos.toString());
            return RespBean.error(ErrorCodeEnum.插入记录失败.getCode(), ErrorCodeEnum.插入记录失败.getMsg(),errIfi1006Dtos);
        }
        return RespBean.success(SucessCodeEnum.插入记录成功.getCode(),SucessCodeEnum.插入记录成功.getMsg());
    }

    private boolean checkParameter(Ifi1006Dto ifi1006Dto){
        return true;
    }
}
